﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Citas.Dominio;
using Citas.Negocio;

//mio mio mio
namespace Citas.Controllers
{
    public class MedicoController : Controller
    {
        IAdministracionService adminService = null;
        //
        // GET: /Cliente/

        public ActionResult Index()
        {
         
            return View(adminService.ListarMedicos());
        }

        public ActionResult Buscar(String nombre)
        {
            ICollection<Medico> modelo = adminService.ListarMedicosXNombre(nombre);
            return View("Index", modelo);
        }
        //
        // GET: /Cliente/Details/5

        public ActionResult Details(int id)
        {
            return View(adminService.ObtenerMedico(id));
        }

        //
        // GET: /Cliente/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Cliente/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {


            try
            {
                string nombreMedico = collection["Nombre"];
                Medico c = adminService.RegistrarMedico(nombreMedico);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View("Create");
            }
        }
        
        //
        // GET: /Cliente/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View(adminService.ObtenerMedico(id));
        }

        //
        // POST: /Cliente/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                try
                {
                    Medico especialidadAModificar = adminService.ObtenerMedico(id);
                    especialidadAModificar.Nombre = (String)collection["Nombre"];

                    Medico b = adminService.ModificarMedico(
                        especialidadAModificar
                    );

                    return RedirectToAction("Index");

                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(
                    string.Empty, ex.Message);
                    return View("Edit");
                }             
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Cliente/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(adminService.ObtenerMedico(id));
        }

        //
        // POST: /Cliente/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                Medico medicoAEliminar = adminService.ObtenerMedico(id);
                adminService.EliminarMedico(medicoAEliminar);
                TempData["mensaje"] = "Médico " + medicoAEliminar.Nombre + " eliminado exitosamente";
                return RedirectToAction("Index"); // Todo bien, a la lista

            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View("Create");
            }
        }
    }
}
